package cn.th.controller;


import cn.th.common.R;
import cn.th.dto.RouteDto;
import cn.th.entity.Myclass;
import cn.th.service.IRouteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

/**
 * <p>
 * 路线流程表 前端控制器
 * </p>
 *
 * @author cn.th
 * @since 2024-03-30
 */
@Slf4j
@RestController
@RequestMapping("/route")
public class RouteController {
    @Autowired
    private IRouteService routeService;

    /**
     * 查询路线流程表
     * @param page 所在页
     * @param pageSize 该页所携带的数据
     * @param school 发布人所在学校
     * @param name 路线名称
     * @param location 出发地
     * @return
     */
    @GetMapping("/page")
    public R<Map<String,Object>> page(@RequestParam(value = "page") int page,
                                      @RequestParam(value = "pageSize") int pageSize,
                                      @RequestParam(value = "school", required = false) String school,
                                      @RequestParam(value = "name", required = false) String name,
                                      @RequestParam(value = "location", required = false) String location){

        Map<String,Object> data = routeService.queryRoute(page, pageSize, school, name, location);

        return R.success(data,"查询路线流程表成功");
    }

    /**
     * 新增路线流程，并将图片保存到route_image中
     * @param routeDto
     * @return
     */
    @PostMapping
    public R<?> save(@RequestBody RouteDto routeDto) {
        log.info("routeDto:{}", routeDto);
        routeService.saveRouteDto(routeDto);
        return R.success(routeDto,"新增礼品成功");
    }

    /**
     * 根据id删除路线流程表，并删除对应的图片
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(String ids){
        log.info("删除礼品表信息，id为：{}",ids);

        //删除礼品表的数据
        routeService.removeRouteById(ids);

        return R.success("删除路线流程表成功");
    }

    /**
     * 更新路线流程表，并同步更新对应图片
     * @param routeDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody RouteDto routeDto){
        log.info("修改路线流程表信息：{}",routeDto);

        routeService.updateRouteById(routeDto);

        return R.success("修改路线流程表成功");
    }

    /**
     * 修改路线时页面的数据展示
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<RouteDto> getRouteDtoById(@PathVariable("id")Long id){
        RouteDto routeDto = routeService.getRouteDtoById(id);
        return R.success(routeDto);
    }
}

